package utils;

import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.io.SVNFileRevision;
import org.tmatesoft.svn.core.io.SVNRepository;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 学习代码
 */
public class Test {

    private SVNRepository repository = null;

    public static SVNDirEntry getTargetInfo(SVNRepository repository, String path, long revision)
    {
        SVNDirEntry collect = null;
        try
        {
            collect = repository.info(path, revision);
            System.err.println(1);
        }
        catch (SVNException e)
        {
            collect = null;
            e.printStackTrace();
        }
        return collect;
    }

    public SvnLogFileInfo getLastFileRevision(SvnLogFileInfo fInfo)
    {
        SvnLogFileInfo fileInfo = null;
        List<SVNFileRevision> s = new ArrayList<SVNFileRevision>();
        int index = -2;
        try
        {
            this.repository.getFileRevisions(fInfo.getFilename(), s, 0, fInfo.getRevision());
        }
        catch (SVNException e)
        {
            e.printStackTrace();
        }

        index = s.size() + index;

        if (index >= 0)
        {
            SVNFileRevision revision = s.get(index);
            fileInfo = new SvnLogFileInfo();
//            fileInfo.setFilename(revision.getPath(), this.getRepositoryType());
            fileInfo.setRevision(revision.getRevision());
        }

        return fileInfo;
    }

    public boolean copyFile(String workspace, String type, SvnLogFileInfo fileInfo)
    {

        FileOutputStream fo = null;
        boolean isSuccess = false;
        try
        {
            SVNProperties pro = new SVNProperties();
            File file = new File(workspace + "\\" + type + "\\" + fileInfo.getShortFileName());
            fo = new FileOutputStream(file);
            try
            {
                this.repository.getFile(fileInfo.getFilename(), fileInfo.getRevision(), pro, fo);
            }
            catch (SVNException e)
            {
                System.err.println("query the file "+fileInfo.getShortFileName()+"'info from repository, but file copy failed!");
            }
            fo.close();
            isSuccess = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (fo != null)
                {
                    fo.close();
                }
            }
            catch (IOException e)
            {
                System.err.println(e.getMessage());
            }
        }
        return isSuccess;
    }

    public long getLatestRevision()
    {
        long lastRevision = 0;
        try
        {
            lastRevision = this.repository.getLatestRevision();
        }
        catch (SVNException e)
        {
            System.err.println("Lastest revision of this repository is not exists!");
        }
        return lastRevision;
    }

    public String getSvnRoot()
    {
        SVNURL url = null;
        try
        {
            url = this.repository.getRepositoryRoot(false);
            return url.getPath();
        }
        catch (SVNException e)
        {
            e.printStackTrace();
            System.err.println("You have no right get this repository root url !!!");
        }
        return null;
    }

    public String[] getCodePaths(String url)
    {
        String codepath = getSvnRoot();
        int homeIndex = url.indexOf(codepath);
        return new String[] { url.substring(homeIndex + codepath.length()) };
    }

    public String getRepositoryType() {

//        return RepositoryOperation.IREP_TYPE_SVN;
        return "";
    }
}
